from django.db import models

from utils.model import BaseModel
from django.conf import settings
from user.models import User

class Video(BaseModel):
    title = models.CharField(max_length=64, verbose_name="视频标题")
    other_title = models.CharField(max_length=64, verbose_name="视频标题别名", blank=True, null=True)
    score = models.DecimalField(max_digits=5, decimal_places=1, verbose_name="视频评分", default=0)
    image = models.ImageField(upload_to="video/", max_length=255, verbose_name="视频封面图片", blank=True, null=True)
    heat = models.IntegerField(default=0, verbose_name="视频热度")
    order = models.IntegerField(default=0, verbose_name="显示顺序")
    desc = models.TextField(max_length=255, verbose_name="视频简介", blank=True, null=True)
    price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="视频价格", default=0)
    is_free = models.BooleanField(default=1, verbose_name="是否免费")
    sections = models.IntegerField(verbose_name="总剧集数量", default=1)
    pub_sections = models.IntegerField(verbose_name="剧集更新数量", default=1)
    pub_time = models.DateField(verbose_name="视频发布时间", blank=True, null=True)
    star = models.IntegerField(verbose_name="视频星星数",default=0)
    search = models.IntegerField(default=0, verbose_name="搜索次数")

    # 视频演职员表
    credits = models.ManyToManyField(to="Credits", through="Video_Credits", through_fields=("video", "credits"))
    category = models.ForeignKey(to="Category", db_constraint=False, on_delete=models.DO_NOTHING, verbose_name="视频类别",blank=True, null=True)
    region = models.ForeignKey(to="Region", db_constraint=False, on_delete=models.DO_NOTHING, verbose_name="视频地区",blank=True, null=True)
    period = models.ForeignKey(to="Period", db_constraint=False, on_delete=models.DO_NOTHING, verbose_name="视频年份",blank=True, null=True)
    # 视频类型
    type = models.ManyToManyField(to="Type", db_constraint=False)

    @property
    def sections_status(self):
        if self.sections == self.pub_sections:
            if self.category.name == "电影":
                return "正片"
            elif self.category.name == "电视剧":
                return f"{self.sections}集全"
            elif self.category.name == "综艺":
                return f"{self.sections}期全"
            elif self.category.name == "动漫":
                return f"{self.sections}话全"
            elif self.category.name == "纪录片":
                return f"{self.sections}集全"
            else:
                return f"{self.sections}集全"
        else:
            if self.category.name == "电视剧":
                return f"更新至{self.pub_sections}集"
            elif self.category.name == "综艺":
                return f"更新至{self.pub_sections}期"
            elif self.category.name == "动漫":
                return f"更新至{self.pub_sections}话"
            elif self.category.name == "纪录片":
                return f"更新至{self.pub_sections}集"
            else:
                return f"更新至{self.pub_sections}集"

    @property
    def category_name(self):
        return self.category.name

    @property
    def region_name(self):
        return self.region.name

    @property
    def period_name(self):
        return self.period.name

    @property
    def types_name(self):
        type_list = self.type.all().values_list("name")
        types_name = []
        for type in type_list:
            types_name.append(type[0])
        return types_name

    @property
    def get_directors(self):
        directors = Video_Credits.objects.filter(video=self,role_id=1).\
            all().values_list("credits__name")
        directors_name = []
        for director in directors:
            directors_name.append(director[0])
        return directors_name

    @property
    def get_casts(self):
        casts = Video_Credits.objects.filter(video=self,role_id=2).\
            all().values_list("credits__name")
        casts_name = []
        for cast in casts:
            casts_name.append(cast[0])
        return casts_name

    @property
    def get_authors(self):
        authors = Video_Credits.objects.filter(video=self,role_id=3).\
            all().values_list("credits__name")
        authors_name = []
        for author in authors:
            authors_name.append(author[0])
        return authors_name

    @property
    def get_section_pks(self):
        section_list = []
        section_pks = self.video_section_set.values_list("v_id")
        for pk in section_pks:
            section_list.append(pk[0])
        return section_list

    @property
    def get_section_pubTime(self):
        section_list = []
        section_pubTime_list = self.video_section_set.values_list("pub_time")
        for time in section_pubTime_list:
            section_list.append(time[0])
        return section_list

    @property
    def get_section_names(self):
        section_list = []
        section_names = self.video_section_set.values_list("name")
        for name in section_names:
            section_list.append(name[0])
        return section_list

    @property
    def image_url(self):
        return f"{settings.BASE_URL}/media/{self.image}"

    class Meta:
        db_table = 'db_video'
        # 后台显示的表名
        verbose_name_plural = '视频表'

    def __str__(self):
        return self.title


class Video_Section(BaseModel):
    v_id = models.AutoField(primary_key=True, verbose_name="剧集主键")
    name = models.CharField(max_length=64, verbose_name="视频剧集名", blank=True, null=True)
    desc = models.CharField(max_length=255, verbose_name="剧集简介", blank=True, null=True)
    total_time = models.IntegerField(verbose_name="剧集时长", blank=True, null=True)
    pub_time = models.DateField(verbose_name="剧集发布时间", blank=True, null=True)
    link = models.CharField(max_length=255, verbose_name="剧集链接", blank=True, null=True)
    order = models.IntegerField(default=0, verbose_name="显示顺序")
    image = models.ImageField(upload_to="section/", max_length=255, verbose_name="视频剧集封面", blank=True, null=True)

    video = models.ForeignKey(to="Video",on_delete=models.DO_NOTHING, db_constraint=False,
                              verbose_name="视频主键",blank=True, null=True)

    @property
    def image_url(self):
        return f"{self.image}"

    class Meta:
        db_table = 'db_video_section'
        # 后台显示的表名
        verbose_name_plural = '视频剧集表'

    def __str__(self):
        return self.name


class Credits(BaseModel):
    name = models.CharField(max_length=32, verbose_name="姓名")
    gender = models.IntegerField(choices=((0, "男"), (1, "女")), verbose_name="性别")
    desc = models.TextField(max_length=250, verbose_name="个人简介", blank=True, null=True)

    class Meta:
        db_table = 'db_credits'
        # 后台显示的表名
        verbose_name_plural = '演职员表'

    def __str__(self):
        return self.name


# 视频演职员表
class Video_Credits(BaseModel):
    video = models.ForeignKey(to="Video", verbose_name="视频主键", on_delete=models.DO_NOTHING,
                              db_constraint=False,blank=True, null=True)
    credits = models.ForeignKey(to="Credits", verbose_name="演职员主键", on_delete=models.DO_NOTHING,
                                db_constraint=False,blank=True, null=True)
    role = models.ForeignKey(to="Role", verbose_name="演职员角色主键", on_delete=models.DO_NOTHING,
                             db_constraint=False,blank=True, null=True)


class Role(BaseModel):
    name = models.CharField(max_length=16, verbose_name="演职角色名")
    order = models.IntegerField(default=0, verbose_name="显示顺序")

    class Meta:
        db_table = 'db_role'
        # 后台显示的表名
        verbose_name_plural = '演职员角色表'

    def __str__(self):
        return self.name


class Category(BaseModel):
    name = models.CharField(max_length=16, verbose_name="视频类别名")
    order = models.IntegerField(default=0, verbose_name="显示顺序")


    class Meta:
        db_table = 'db_category'
        # 后台显示的表名
        verbose_name_plural = '视频类别表'

    def __str__(self):
        return self.name


class Region(BaseModel):
    name = models.CharField(max_length=16, verbose_name="视频地区名")
    order = models.IntegerField(default=0, verbose_name="显示顺序")


    class Meta:
        db_table = 'db_region'
        # 后台显示的表名
        verbose_name_plural = '视频地区表'

    def __str__(self):
        return self.name


class Type(BaseModel):
    name = models.CharField(max_length=16, verbose_name="视频类型名")
    order = models.IntegerField(default=0, verbose_name="显示顺序")


    class Meta:
        db_table = 'db_type'
        # 后台显示的表名
        verbose_name_plural = '视频类型表'

    def __str__(self):
        return self.name


class Period(BaseModel):
    name = models.CharField(max_length=16, verbose_name="视频年份名")
    order = models.IntegerField(default=0, verbose_name="显示顺序")


    class Meta:
        db_table = 'db_period'
        # 后台显示的表名
        verbose_name_plural = '视频年份表'

    def __str__(self):
        return self.name


# 观看视频记录表
class Record(BaseModel):
    user = models.ForeignKey(to=User, verbose_name="用户主键", on_delete=models.CASCADE,
                              db_constraint=False,blank=True, null=True)
    video_section = models.ForeignKey(to="Video_Section", verbose_name="视频剧集主键", on_delete=models.DO_NOTHING,
                              db_constraint=False,blank=True, null=True)
    play_time = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="视频剧集播放时间", default=0)

    @property
    def section_info(self):
        section_info = {}
        section_info["id"] = self.video_section.v_id
        section_info["name"] = self.video_section.name
        if self.video_section.total_time:
            total_time = int(self.video_section.total_time)
            hour = total_time // 60
            min = total_time - (hour*60)
            section_info["total_time"] = f"{hour}:{min}:00"
        else:
            section_info["total_time"] = "未知时长"
        section_info["image"] = f"{self.video_section.image}"
        return section_info

    @property
    def video_id(self):
        return self.video_section.video.id

    class Meta:
        db_table = 'db_record'
        # 后台显示的表名
        verbose_name_plural = '视频观看记录表'

    def __str__(self):
        return f"{self.user}观看{self.video_section}至{self.play_time}秒"